package com.thoughtworks.turtlemock.filemover;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileMover {
    private final FileAdaptee destDir;
    private final List rollbackables = new ArrayList();

    public FileMover(FileAdaptee aDir) {
        this.destDir = aDir;
    }

    public void move(final FileAdaptee file) {
        String destFilePath = destDir.getAbsolutePath() + File.separator + file.getName();
        final FileAdaptee destFile = file.moveTo(destFilePath);
        rollbackables.add(new Rollbackable() {
            public void rollback() {
                destFile.moveTo(file.getAbsolutePath());
            }
        });
    }

    public void rollback() {
        for (int i = 0; i < rollbackables.size(); i++) {
            Rollbackable rollbackable = (Rollbackable) rollbackables.get(i);
            rollbackable.rollback();
        }
    }
}
